<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入数据库
use think\Db;

use think\Request;

class Product extends Backend {
    //默认配置

    public function __construct() {

        // 继承父类
        parent::__construct();

        // 引入模型
        $this->ConfigModel = model( 'Config' );
        $this->UserModel = model( 'User.User' );
        $this->RechargeModel = model( 'User.Recharge' );
        $this->ProductModel = model( 'Product.Product' );
        $this->ProtypeModel = model( 'Product.Type' );
    }

    // 商品列表

    public function index() {
        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'product.createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'product.createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'product.createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'product.name' ] = [ 'like', "%$keywords%" ];
        }

        $count = $this->ProductModel->count();
        $productlist = $this->ProductModel->with( [ 'type', 'vip' ] )->where( $where )->order( 'typeid', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        foreach ( $productlist as $item ) {
            $item[ 'content' ] = htmlspecialchars( $item[ 'content' ] );
        }

        $productstr = $productlist->render();

        $this->assign( [
            'count' => $count,
            'productlist' => $productlist,
            'productstr' => $productstr,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();
    }

    // 修改商品信息

    public function edit() {

        $proid = $this->request->param( 'id', 0 );
        if ( $proid === 0 ) {
            $this->error( '获取商品id失败！' );
            exit;
        }

        // 获取商品信息
        $proinfo = $this->ProductModel->with( [ 'type', 'vip' ] )->where( [ 'product.id' => $proid ] )->find();
        $proinfo[ 'thumbs' ] = explode( ',', $proinfo[ 'thumbs' ] );

        // 获取类别信息
        $type = $this->ProtypeModel->select();

        $this->assign( [
            'proinfo' => $proinfo,
            'type' => $type
        ] );

        if ( $this->request->isPost() ) {

            $typeid = $this->request->param( 'pro_type', 0 );
            $name = $this->request->param( 'pro_name', '' );
            $price = $this->request->param( 'pro_price', '' );
            $stock = $this->request->param( 'pro_stock', '' );
            $flag = $this->request->param( 'pro_flag', '' );
            $content = $this->request->param( 'content', '' );

            $data = [
                'id' => $proid,
                'typeid' => $typeid,
                'name' => $name,
                'price' => $price,
                'stock' => $stock,
                'flag' => $flag,
                'content' => $content,
            ];

            $thumbsImgURL = [];

            // 图集上传
            if ( empty( request()->file( 'thumbs' ) ) ) {
                $thumbsImgURL = isset( $params[ 'thumbs' ] ) ? $params[ 'thumbs' ] : '';
            } else {
                // 头像图片上传
                $thumbsImgURL = uploads( 'thumbs' );

                if ( $thumbsImgURL === false ) {
                    $this->error( '修改图集失败！' );
                    exit;
                }

                $thumbsStr = '';
                foreach ( $thumbsImgURL as $item ) {
                    $thumbsStr .= 'uploads/'.$item.',';
                }

                $thumbsStr = trim( $thumbsStr, ',' );
            }

            // var_dump( $thumbsStr );

            $data[ 'thumbs' ] = $thumbsStr;

            // 修改数据库
            $res = $this->ProductModel->isUpdate()->save( $data );

            if ( $res === false ) {

                // 如果更新失败将上传的图片删除
                foreach ( $thumbsImgURL as $item ) {
                    @is_file( 'uploads/'.$item ) && @unlink( 'uploads/'.$item );
                }

                $this->error( $this->ProductModel->getError() );
                exit;
            } else {
                // 修改成功后，删除原来的图片
                foreach ( $proinfo[ 'thumbs' ] as $item ) {
                    $img = ltrim( $item, '/' );
                    @is_file( $img ) && @unlink( $img );
                }

                $this->success( '修改商品信息成功！', url( 'admin/product/index' ) );
                exit;
            }

        }

        return $this->fetch();

    }

    // 添加商品

    public function add() {
        // 获取类别信息
        $type = $this->ProtypeModel->select();

        $this->assign( [
            'type' => $type
        ] );
        if ( $this->request->isPost() ) {

            $typeid = $this->request->param( 'pro_type', 0 );
            $name = $this->request->param( 'pro_name', '' );
            $price = $this->request->param( 'pro_price', '' );
            $stock = $this->request->param( 'pro_stock', '' );
            $flag = $this->request->param( 'pro_flag', '' );
            $content = $this->request->param( 'content', '' );

            $data = [
                'typeid' => $typeid,
                'name' => $name,
                'price' => $price,
                'stock' => $stock,
                'flag' => $flag,
                'content' => $content,
            ];

            $thumbsImgURL = [];

            // 图集上传
            if ( empty( request()->file( 'thumbs' ) ) ) {
                $thumbsImgURL = isset( $params[ 'thumbs' ] ) ? $params[ 'thumbs' ] : '';
            } else {
                // 头像图片上传
                $thumbsImgURL = uploads( 'thumbs' );

                if ( $thumbsImgURL === false ) {
                    $this->error( '修改图集失败！' );
                    exit;
                }

                $thumbsStr = '';
                foreach ( $thumbsImgURL as $item ) {
                    $thumbsStr .= 'uploads/'.$item.',';
                }

                $thumbsStr = trim( $thumbsStr, ',' );
            }

            // var_dump( $thumbsStr );

            $data[ 'thumbs' ] = $thumbsStr;

            $res = $this->ProductModel->save( $data );

            if ( $res === false ) {
                // 插入失败
                // 上传上来的图片删除了
                foreach ( $thumbsImgURL as $item ) {
                    @is_file( 'uploads/'.$item ) && @unlink( 'uploads/'.$item );
                }

                $this->error( $this->ProductModel->getError() );
            } else {
                $this->success( '商品添加成功！', 'admin/product/index' );
                exit;
            }

        }
        return $this->fetch();
    }

    /* 删除商品 */

    public function del() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            // 软删除
            $result = $this->ProductModel->destroy( $ids );

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ProductModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '添加回收站成功！';
            }

            return json( $success );
        }
    }

    // 回收站

    public function recycle() {
        // 获取开始时间
        $start = $this->request->param( 'start', '' );

        // 获取结束时间
        $end = $this->request->param( 'end', '' );

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $start ) {
            // 加入条件 大于开始时间
            $where[ 'product.createtime' ] = [ '> time', $start ];
        }

        if ( $end ) {
            // 加入条件 小于或等于结束时间
            $where[ 'product.createtime' ] = [ '<= time', $end ];
        }

        // 若两个都存在就将两个条件拼接
        if ( $start && $end ) {
            $where[ 'product.createtime' ] = [ 'between time', [ $start, $end ] ];
        }

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'product.name' ] = [ 'like', "%$keywords%" ];
        }

        $num = $this->ProductModel->onlyTrashed()->select();
        $count = count( $num );
        $productlist = $this->ProductModel->onlyTrashed()->with( [ 'type', 'vip' ] )->where( $where )->order( 'typeid', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        foreach ( $productlist as $item ) {
            $item[ 'content' ] = htmlspecialchars( $item[ 'content' ] );
        }

        $productstr = $productlist->render();

        $this->assign( [
            'count' => $count,
            'productlist' => $productlist,
            'productstr' => $productstr,
            'keywords' => $keywords,
            'start' => $start,
            'end' => $end,
        ] );

        return $this->fetch();
    }

    // 取消软删除 恢复正常

    public function recovery() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            $data = [
                'deletetime' => null,
            ];

            // 软删除
            $result = $this->ProductModel->onlyTrashed()->where( $where )->update( [ 'deletetime' => NULL ] );

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ProductModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '恢复商品成功';
            }

            return json( $success );
        }

    }

    // 真正删除

    public function cut() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            // 软删除
            $result = $this->ProductModel->onlyTrashed()->where( $where )->delete();

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ProductModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除商品成功';
            }

            return json( $success );
        }
        return '456';
    }

}
?>